package com.yulongtian.oneProblemEveryDay.month202211.day20221129;

/**
 * https://leetcode.cn/problems/minimum-amount-of-time-to-collect-garbage/
 * 可
 *
 * @author yulongTian
 * @create 2022-11-29 17:34
 */
public class Test07 {
    public static void main(String[] args) {

    }

    public int garbageCollection(String[] garbage, int[] travel) {
        int lenHouse = garbage.length;

        int preMTime = 0;//目前M所需的时间
        int tempMTime = 0;//还未收拾M但如果收拾就需要的时间
        int prePTime = 0;//目前P所需的时间
        int tempPTime = 0;//还未收拾P但如果收拾就需要的时间
        int preGTime = 0;//目前G所需的时间
        int tempGTime = 0;//还未收拾G但如果收拾就需要的时间

        //先处理第一家
        String garbage1 = garbage[0];
        for (int i = 0; i < garbage1.length(); i++) {
            char c = garbage1.charAt(i);
            if (c == 'M') {
                preMTime += 1;
            }
            if (c == 'P') {
                prePTime += 1;
            }
            if (c == 'G') {
                preGTime += 1;
            }
        }

        for (int i = 1; i < lenHouse; i++) {
            //过路时间
            tempMTime += travel[i - 1];
            tempPTime += travel[i - 1];
            tempGTime += travel[i - 1];

            //收拾垃圾
            for (int j = 0; j < garbage[i].length(); j++) {
                char c = garbage[i].charAt(j);
                if (c == 'M') {
                    preMTime += 1;
                    preMTime += tempMTime;
                    tempMTime = 0;
                }
                if (c == 'P') {
                    prePTime += 1;
                    prePTime += tempPTime;
                    tempPTime = 0;
                }
                if (c == 'G') {
                    preGTime += 1;
                    preGTime += tempGTime;
                    tempGTime = 0;
                }
            }

        }


        return preGTime + preMTime + prePTime;
    }

}
